package com.dsjy.yf.address.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsjy.yf.address.config.base.BizException;
import com.dsjy.yf.address.config.base.CommonEnum;
import com.dsjy.yf.address.entity.dataobject.BuildingDO;
import com.dsjy.yf.address.entity.myenum.OperateEnum;
import com.dsjy.yf.address.mapper.BuildingMapper;
import com.dsjy.yf.address.service.BuildingService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author whj
 * @version 1.0
 * @date 2021-01-19 20:47
 */
@Service
public class BuildingServiceImpl implements BuildingService {
    @Resource
    private BuildingMapper buildingMapper;



    @Override
    @Cacheable(value = "community", key = "'searchBuildingWithCode'+#parentCode+#flag")
    public List<BuildingDO> searchBuildingWithCode(Integer parentCode, Boolean flag) {
        return buildingMapper.searchBuildingWithCode(parentCode, flag);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "community", condition = "#buildingDO.parentCode != null", key = "'searchBuildingWithCode'+#buildingDO.parentCode+'false'"),
            @CacheEvict(value = "community", condition = "#buildingDO.parentCode != null", key = "'searchBuildingWithCode'+#buildingDO.parentCode+'true'")
    })
    @Transactional(rollbackFor = RuntimeException.class)
    public Integer addUpdDel(String operate, BuildingDO buildingDO) {
        Integer result = null;
        if (OperateEnum.ADD.getOperate().equals(operate) && buildingDO.getBuildingName() != null) {
            result = addBuilding(buildingDO);
        } else if (OperateEnum.UPDATE.getOperate().equals(operate) && buildingDO.getId() != null) {
            result = updBuilding(buildingDO);
        }
        return result;
    }

    @Override
    public Integer addBuilding(BuildingDO buildingDO) {
        if (buildingDO.getParentCode() == null || String.valueOf(buildingDO.getParentCode()).length() != 6) {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
        buildingDO.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return buildingMapper.addBuilding(buildingDO);
    }


    @Override
    public Integer updBuilding(BuildingDO buildingDO) {
        buildingDO.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return buildingMapper.updBuilding(buildingDO);
    }

    @Override
    public IPage searchWithPage(Integer pageNum, Integer pageSize, QueryWrapper wrapper) {
        wrapper.setEntityClass(BuildingDO.class);
        Page<BuildingDO> page = new Page<>(pageNum, pageSize);
        return buildingMapper.selectPage(page, wrapper);
    }
}
